//
//  EditPassWordViewController.m
//  QA
//
//  Created by 李小龙 on 2017/5/17.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "EditPassWordViewController.h"

@interface EditPassWordViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UITextField *testTF;
@property (weak, nonatomic) IBOutlet UIButton *testBtn;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordAgainTF;

@property (strong, nonatomic) NSTimer *myTimer;
@property (assign, nonatomic) NSInteger num;
@end

@implementation EditPassWordViewController

- (IBAction)getTestNumber:(id)sender {
    UIButton *button = (UIButton *)sender;
    button.userInteractionEnabled = NO;
    [button setTitleColor:[UIColor grayTextColor] forState:UIControlStateNormal];
    
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    NSString *originTel = [UserInfo shareInstance].user.mtel;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:originTel forKey:@"mobile"];
    
    [MYAFNetworking POST:URL_SMS parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dics = responseObject;
        if ([[dics objectForKey:@"code"] isEqualToString:@"200"]) {
            [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"验证码发送成功!" animated:YES];
        } else {
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"修改密码";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor colorWithHex:0x030303 alpha:1.0] forKey:NSForegroundColorAttributeName];
    [right setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    [self createUI];
    
}
- (void)rightBarButtonItemAction {
    
    if (_passWordTF.text.length < 6 || _passWordTF.text.length > 16) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请输入新密码，密码为6~16位的英文加数字" animated:YES];
        return;
    }
    if (![_passWordAgainTF.text isEqualToString:_passWordTF.text]) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"两次输入的密码不一致" animated:YES];
        return;
    }
    //提交接口
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    [dic setValue:self.passWordTF.text forKey:@"mpass"];
    [dic setValue:self.passWordAgainTF.text forKey:@"qrpass"];
    [dic setValue:self.testTF.text forKey:@"verificationcode"];
    __weak typeof(self) my = self;
    [MYAFNetworking POST:URL_Save_Mpass parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dics = responseObject;
        if ([[dics objectForKey:@"code"] isEqualToString:@"200"]) {
            
            [my disMissSelf];
            
        }else{
            [MBProgressHUD myShowHUDAddedTo:self.view labelText:[dics objectForKey:@"message"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        
    }];
}
- (void)createUI {
    //从个人信息中取出手机号
    NSString *phone = [LXLIsPhoneNumber phoneNumber:[UserInfo shareInstance].user.mtel];
    NSString *phoneLabelText = [NSString stringWithFormat:@"已将验证码发送至您绑定的手机号%@，请注意查收短信",phone];
    [self.phoneLabel setText:phoneLabelText];
    
    [self.backView setShadowWith:nil opacity:0 offset:CGSizeMake(0, 4)];
    
    self.num = 60;
    [self getTestNumber:self.testBtn];
}

- (void)timerAction{
    [_testBtn setTitle:[NSString stringWithFormat:@"%lds",(long)self.num--] forState:UIControlStateNormal];
    
    if (_num == 0) {
        [_testBtn setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
        _testBtn.userInteractionEnabled = YES;
        _num = 60;
        [_myTimer invalidate];
    }
}


#pragma mark - textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if ([string isEqualToString:@" "]) {
        return NO;
    }
    if (textField == self.testTF) {
        if (textField.text.length >= 4) {
            return NO;
        } else {
            return YES;
        }
    }
    if (textField == self.passWordTF || textField == self.passWordAgainTF) {
        if (textField.text.length >= 16 || [string isNumAndChar] == NO) {
            return NO;
        } else {
            return YES;
        }
    }
    return YES;
}

- (void)dealloc {
    [self.myTimer invalidate];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
